<?php

namespace admin\controller;
class code extends _base
{
    public function __construct()
    {
        parent::__construct();
        $this->assign('m','activity');
        $this->assign('y','code');
    }

    /**
     * @Notes:列表
     * @Interface lists
     * @author: LZH
     * @Time: 2019/6/18   17:30
     */
    public function lists()
    {

        $aParams = array();
        //标题
        if ($code = I('code')) {
            $aParams['where']['code LIKE'] = "%{$code}%";
        }


        $aParams['page'] = max(1, intval(I('page')));
        $aData = M('code')->page($aParams);
        $this->display('activity/code_lists',['aData'=>$aData]);
    }

    /**
     * @Notes:添加
     * @Interface add
     * @author: LZH
     * @Time: 2019/6/18   17:26
     */
    public function add()
    {
        //表单提交操作
        if (I('act') == 'do') {
            $info = array(
                'uniacid'=>uniacid(),
                'code' => I('code'),
                'rand' => I('rand'),
                'stock_num' => I('stock_num'),
                'creation_date' => time(),
            );

            if ( M('code')->getData(['field'=>['code_id'],'where'=>['code' => $info['code']]]))
                create_info('该商品码已存在', '', 'error');

            if (!$iId = M('code')->add($info))
                create_info('商品码添加失败', '', 'error');

            //操作记录待补充
            $this->log_act('添加商品码 ID：' . (int)$iId);
            create_info('添加成功', gWebUrl('code/lists'), 'success');

        }

        $this->display('activity/code_info');
    }

    /**
     * @Notes:修改
     * @Interface edit
     * @author: LZH
     * @Time: 2019/6/18   17:31
     */
    public function edit()
    {
        //表单提交操作
        if (I('act') == 'do') {
            $info = array(
                'code' => I('code'),
                'rand' => I('rand'),
                'stock_num' => I('stock_num'),
                'last_update' => time(),
            );
            //where条件
            $aParams['where']['code_id'] = I('code_id');
            if ( M('code')->getData(['field'=>['code_id'],'where'=>['code' => $info['code'],'code_id !=' =>I('code_id') ]]))
                create_info('该商品码已存在', '', 'error');

            if (!M('code')->edit($info,$aParams))
                create_info('修改商品码失败', '', 'error');

            //操作记录待补充
            $this->log_act('修改商品码 ID：' . (int)I('code_id'));
            create_info('成功修改商品码信息',gWebUrl('code/lists'),'success');
        }

        $aParams['where'] = ['code_id' => I('code_id')];
        if (!$aData = M('code')->getData($aParams))
            create_info('该商品码不存在', '', 'error');

        $this->display('activity/code_info',['aData'=>$aData]);
    }


    /**
     * @Notes:删除
     * @Interface del
     * @author: LZH
     * @Time: 2019/6/18   17:33
     */
    public function del()
    {
        $iId = I('code_id');
        $aParams['where'] = array('code_id' => $iId);
        if (!$aData = M('code')->getData($aParams))
            create_info('该商品码不存在', '', 'error');
        if (!M('code')->del($aParams))
            create_info('删除失败', '', 'error');

        $this->log_act('删除商品码 ID：' . (int)$iId);
        create_info('删除成功', gWebUrl('code/lists'), 'success');
    }

    public function del_all(){
        $iId = I('ids');
        $aParams['where'] =['code_id in'=>$iId];
        if (!M('code')->del($aParams))
            create_info('删除失败', '', 'error');

        $this->log_act('删除商品码 ID：' . $iId);
        create_info('删除成功', gWebUrl('code/lists'), 'success');
    }


}

